package com.clouddot.baipai.presenter;


import com.clouddot.baipai.contract.IMainFragmentContract;
import com.clouddot.baipai.http.entity.response.getArcticByIdResponse;
import com.clouddot.baipai.http.entity.response.getArcticListResponse;
import com.clouddot.baipai.http.entity.response.getClassInfoByIdResponse;
import com.clouddot.baipai.http.entity.response.getClassInfoListResponse;
import com.clouddot.baipai.http.entity.response.getCurrentCourseResponse;

import org.wxy.sdk.base.impl.BasePresenterImpl;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainFragmentPresenter extends BasePresenterImpl<IMainFragmentContract.IView, IMainFragmentContract.IModel> implements IMainFragmentContract.IPresenter {
    public MainFragmentPresenter(IMainFragmentContract.IView view, IMainFragmentContract.IModel model) {
        super(view, model);
    }


    @Override
    public void getClassInfoById(String id) {
        Subscription subscription = mModel.getClassInfoById("QueryTaskDetail", id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getClassInfoByIdResponse>() {
                               @Override
                               public void call(getClassInfoByIdResponse response) {
                                   mView.success(1, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(1, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getClassInfo(final int type, String TaskType, String ClassId, int pageindex, int pagesize) {
        Subscription subscription = mModel.getClassInfo("QueryTaskCardPage", TaskType, ClassId, pageindex, pagesize)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getClassInfoListResponse>() {
                               @Override
                               public void call(getClassInfoListResponse response) {

                                       response.flag = type;
                                       mView.success(2, response);

                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(2, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getCurrentCourse(String ClassId) {
        Subscription subscription = mModel.getCurrentCourse("QueryCurrentClassTeacher", ClassId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getCurrentCourseResponse>() {
                               @Override
                               public void call(getCurrentCourseResponse response) {
                                   if (!response.WeekNo.equals("0")) {
                                       mView.success(3, response);
                                   } else {
                                       mView.fail(3, "当前时间无课程");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(3, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);

    }

    @Override
    public void getArcticList(String type, int pageindex, int pagesize) {
        Subscription subscription = mModel.getArcticList("QueryCmsPage", type, pageindex, pagesize)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getArcticListResponse>() {
                               @Override
                               public void call(getArcticListResponse response) {
                                   mView.success(4, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(4, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getArcticById(String id) {
        Subscription subscription = mModel.getArcticById("GetCmsById", id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getArcticByIdResponse>() {
                               @Override
                               public void call(getArcticByIdResponse response) {
                                   mView.success(5, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(5, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }
}
